<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\User;
use think\Config;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');

        /*$beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $dateStr = date('Y-m-d', time());
        $timestamp24 = strtotime($dateStr) + 86400;*/

        $usercount = User::count('id');
        $dd = \app\common\model\GxHistory::count('id');
        $moneysum = User::sum('money');
        $scoresum = User::sum('score');
        $todayregiser = User::whereTime('createtime','today')->count('id');
        $todaylogin = User::whereTime('logintime','today')->count('id');
        $todaydd = \app\common\model\GxHistory::whereTime('created_at','today')->count('id');
        $status = \app\common\model\GxHistory::where('status','0')->count('id');
        $weekuser = User::whereTime('createtime','week')->count('id');
        $weeklively = User::whereTime('logintime','week')->count('id');
        $this->view->assign([
            'totaluser'        => $usercount,
            'totalviews'       => $moneysum,
            'totalorder'       => $dd,
            'totalorderamount' => $scoresum,
            'todayuserlogin'   => $todaylogin,
            'todayusersignup'  => $todayregiser,
            'todayorder'       => $todaydd,
            'unsettleorder'    => $status,
            'sevendnu'         => $weekuser,
            'sevendau'         => $weeklively,
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode
        ]);

        return $this->view->fetch();
    }

}
